<?php

namespace SL\APIBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use SL\APIBundle\Entity\Projet;
use SL\APIBundle\Form\ProjetType;

class ProjetController extends Controller
{
	public function listAction()
	{
		$repository = $this->getDoctrine()->getManager('api')->getRepository('SLAPIBundle:Projet');
	
		$projets = $repository->findAll();
		return $this->render('SLAPIBundle:Projet:list.html.twig', array('projets' => $projets));
	}
	
	public function newAction()
	{
		$projet = new Projet;
		
		$form = $this->createForm(new ProjetType, $projet);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{				
				$em = $this->getDoctrine()->getManager('api');
				$repository = $em->getRepository('SLAPIBundle:Projet');
				
				if ($repository->findOneBy(array('nom' => $projet->getUrl())) != null)
					return $this->render('SLAPIBundle:Projet:new.html.twig', 
									array('projet' => $projet, 'form' => $form->createView(), 'error' => "Ce projet existe déjà. Changez de nom"));
							
				$em->persist($projet);
				$em->flush();
				
				return $this->redirect($this->generateUrl('sl_api_projet_edit', array('projet_id' => $projet->getId())));
			}
		}
		return $this->render('SLAPIBundle:Projet:new.html.twig', array('projet' => $projet, 'form' => $form->createView(), 'error' => null));
	}

	public function editAction($projet_id)
	{
		$repository = $this->getDoctrine()->getManager('api')->getRepository('SLAPIBundle:Projet');
		
		$projet = $repository->findOneBy(array('id' => intval($projet_id)));
		if ($projet === null)
		{
			throw $this->createNotFoundException('Projet inexistant.');
		}
		
		$form = $this->createForm(new ProjetType, $projet);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{
				
				$em = $this->getDoctrine()->getManager();
				$em->persist($projet);	
				$em->flush();
				
				return $this->redirect($this->generateUrl('sl_api_projet_edit', array('projet_id' => $projet_id)));
			}
		}
				
		return $this->render('SLAPIBundle:Projet:edit.html.twig', 
									array('projet' => $projet, 'form' => $form->createView()));
	}
	
	public function deleteAction($projet_id)
	{
		$repository = $this->getDoctrine()->getManager('api')->getRepository('SLAPIBundle:Projet');
		
		$projet = $repository->findOneBy(array('id' => intval($projet_id)));
		if ($projet === null)
		{
			throw $this->createNotFoundException('Projet inexistant.');
		}
		
		$form = $this->createFormBuilder()->getForm();
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			$em = $this->getDoctrine()->getManager('api');
			
			$users = $projet->getUsers();	
			foreach ($users as $user)
			{
				$em->remove($user->getLot());
				$em->remove($user);
			}
			$em->remove($projet);
			$em->flush();
				
			$this->get('session')->getFlashBag()->add('Administration', 'Le Projet '.$projet->getName().' a bien été supprimé');
				
			return $this->redirect($this->generateUrl('sl_api_projet_list'));
			
		}
				
		return $this->render('SLAPIBundle:Projet:delete.html.twig', 
									array('projet' => $projet, 'form' => $form->createView()));
	}
	
	public function getProjetAction($projet_id)
	{
		$repository = $this->getDoctrine()->getManager('api')->getRepository('SLAPIBundle:Projet');
	
		$projet = $repository->findOneBy(array('id' => intval($projet_id)));
		$lots = $projet->getLots();
		
		return $this->render('SLAPIBundle:Projet:projet.html.twig', array('projet' => $projet, 'lots' => $lots));
	}
}
